import { Controller } from "../../mvc/core/Controller";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { ServerCommand } from "../mvc/ServerCommand";
import { EGateCommand } from "../../../Message/Command/EGateCommand";
import { DataEvent } from "../events/DataEvent";
import { IView } from "../../mvc/interfaces/IView";
import { VIPModule } from "../../../modules/VIPModule";
import { WindowEvent } from "../../component/window/WindowEvent";
import { GameProxy } from "../mvc/GameProxy";
import { IVIPModule } from "../../../modules/interfaces/IVIPModule";
import { MessageBlock } from "../../../Framework/MQ/MessageBlock";
import { STestVIPCard } from "../../../Message/Game/STestVIPCard";
import { SActiveToClient } from "../../../Message/Public/SActiveToClient";
//class VIPController
    
    export  class VIPController extends Controller
    {
       

        protected /* override */ initServer(): void
        {
            Dispatcher.addEventListener(EventName.StartVIPBless, this.onStartVIPBless);
            Dispatcher.addEventListener(EventName.StopVIPBless, this.onStopVIPBless);
            Dispatcher.addEventListener(EventName.ShowVIPWindow, this.onShowVIPWindow);
            Dispatcher.addEventListener(EventName.GetVIPRewards, this.onGetVIPRewards);
            Dispatcher.addEventListener(EventName.ShowVIPBoonWindow, this.onShowVIPBoonWindow);
            NetDispatcher.addCmdListener(ServerCommand.VIPLoginRewardActiveCommand, this.onVIPLoginReward);
            NetDispatcher.addCmdListener(EGateCommand._ECmdGateTestVIPCard, this.onTestVipCardRes);
            Dispatcher.addEventListener(EventName.Guide_VipGiftGet, this.onGuideVipGiftGetHandler);
            Dispatcher.addEventListener(EventName.ChangeRoleAvatarHeadIcon, this.updateHeadInfo);
            return;
        }

        /* internal  */updateHeadInfo(arg1: DataEvent): void
        {
            if (this._vipModule) 
            {
                this._vipModule.updateHeadInfo();
            }
            return;
        }

        protected /* override */ initView(): IView
        {
            if (!this._vipModule) 
            {
                this._vipModule = new VIPModule();
                this.updateVIPInfo();
                this.updatePlayerBase();
                this._vipModule.addEventListener(WindowEvent.SHOW, this.onVipModuleShowHandler);
                this._vipModule.addEventListener(WindowEvent.CLOSE, this.onVipModuleHideHandler);
            }
            return this._vipModule;
        }

        /* internal  */onStartVIPBless(arg1: DataEvent): void
        {
            GameProxy.vip.startVIPBless();
            return;
        }

        /* internal  */onStopVIPBless(arg1: DataEvent): void
        {
            GameProxy.vip.stopVIPBless();
            return;
        }

        /* internal  */onShowVIPWindow(arg1: DataEvent): void
        {
            view.show();
            return;
        }

        /* internal  */onShowVIPBoonWindow(arg1: DataEvent): void
        {
            view.show();
            (view as IVIPModule).changeToOtherVipBoon(int(arg1.data));
            return;
        }

        /* internal  */onGetVIPRewards(arg1: DataEvent): void
        {
            GameProxy.vip.getVIPRewards(int(arg1.data));
            return;
        }

        /* internal  */updateVIPInfo(): void
        {
            (view as IVIPModule).updateVIPInfo(cache.vip.vipInfo);
            return;
        }

        /* internal  */updatePlayerBase(): void
        {
            (view as IVIPModule).updatePlayerBase(cache.role.entityInfo);
            return;
        }

        /* internal  */onVipInfoUpdate(arg1: Object=null): void
        {
            if (cache.vip.vipInfo) 
            {
                (view as IVIPModule).updateVIPInfo(cache.vip.vipInfo);
            }
            return;
        }

        /* internal  */onVipBlessStateUpdate(arg1: Boolean=false): void
        {
            (view as IVIPModule).setVIPBlessState(cache.vip.isVipBlessStart);
            return;
        }

        /* internal  */onTestVipCardRes(arg1: MessageBlock): void
        {
            cache.vip.vipTestCard = arg1.messageBase as STestVIPCard;
            Dispatcher.dispatchEvent(new DataEvent(EventName.TestVIPCardRes));
            return;
        }

        /* internal  */onVIPLoginReward(arg1: SActiveToClient): void
        {
            this._isHaveVIPLoginReward = true;
            if (this._vipModule) 
            {
                (view as IVIPModule).VIPLoginRewardVisible = true;
            }
            return;
        }

        /* internal  */onGuideVipGiftGetHandler(arg1: DataEvent): void
        {
            this.guideVipGiftGet();
            return;
        }

        /* internal  */guideVipGiftGet(): void
        {
            if (cache.guide.vipLevelUpGuide) 
            {
                cache.guide.vipLevelUpGuide = false;
                (view as IVIPModule).guideVipGiftGet(cache.vip.vipInfo);
            }
            return;
        }

        /* internal  */onVipModuleShowHandler(arg1: WindowEvent): void
        {
            this.onVipInfoUpdate();
            this.onVipBlessStateUpdate();
            (view as IVIPModule).VIPLoginRewardVisible = this._isHaveVIPLoginReward;
            this.guideVipGiftGet();
            NetDispatcher.addCmdListener(ServerCommand.VIPInfoUpdateCommand, this.onVipInfoUpdate);
            NetDispatcher.addCmdListener(ServerCommand.VIPBlessStateChange, this.onVipBlessStateUpdate);
            return;
        }

        /* internal  */onVipModuleHideHandler(arg1: WindowEvent): void
        {
            NetDispatcher.removeCmdListener(ServerCommand.VIPInfoUpdateCommand, this.onVipInfoUpdate);
            NetDispatcher.removeCmdListener(ServerCommand.VIPBlessStateChange, this.onVipBlessStateUpdate);
            return;
        }

        private /* var */_vipModule: IVIPModule;

        private /* var */_isHaveVIPLoginReward: Boolean=false;
    }
